package com.oyesgo.statistics.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

import org.apache.commons.dbcp.BasicDataSource;

import com.oyesgo.statistics.log.LogUtils;

/**
 * @FileName JdbcUtil.java
 * @Description: 数据库直连工具类
 * @author rose
 * @version 1.0
 * <p>Company: OYESGO</p>
 * <p>Copyright: Copyright (c) 2010</p>
 */
public class JdbcUtil {
	//静态常量全部饿汉式初始化
	private static ResourceBundle bundel = ResourceBundle.getBundle("jdbc");
	private static final String DRIVER = bundel.getString("mysql.driver");
	private static final String URL = bundel.getString("mysql.url");
	private static final String USER_NAME = bundel.getString("mysql.userName");
	private static final String PASSWORD = bundel.getString("mysql.password");
	private static BasicDataSource source;
	//加载驱动
	static{
		try {
			Class.forName(DRIVER);
		} catch (ClassNotFoundException e) {
			LogUtils.printlnErrorToFile("[驱动没有找到!]");
			GlobalContent.flag = true;
			e.printStackTrace();
		}
	}
	
	//初始化datasource
	 static{
		 try {
			 if (source == null) {
				 source = new BasicDataSource();
				 source.setDriverClassName(DRIVER);
				 source.setUrl(URL);
				 source.setUsername(USER_NAME);
				 source.setPassword(PASSWORD);
				 source.setInitialSize(5);
				 source.setMaxActive(20);
				 source.setMaxIdle(10);
				 source.setMinIdle(5);
				 source.setRemoveAbandoned(true);
				 source.setRemoveAbandonedTimeout(180);
				 source.setMaxWait(1000);
				 source.setLogAbandoned(true);
			 }
		 } catch (Exception e) {
			 LogUtils.printlnErrorToFile("[datasource创建异常!]");
			 GlobalContent.flag = true;
			 e.printStackTrace();
		 }
	}
	/**
	 * 获得连接
	 * @return 
	 * Connection 
	 * @author rose
	 */
	public static Connection getConnection(){
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
		} catch (SQLException e) {
			LogUtils.printlnErrorToFile("[获取连接失败!]");
			GlobalContent.flag = true;
			e.printStackTrace();
		}
		return conn;
	}
	/**
	 * @return 
	 * Connection 
	 * @author rose
	 */
	public static Connection getConnectionByDbcp() {
		Connection conn = null;
		try {
			conn = source.getConnection();
		} catch (SQLException e) {
			LogUtils.printlnErrorToFile("[dbcp获取连接失败!]");
			GlobalContent.flag = true;
		}
		return conn;
	}
	/**
	 * 关闭连接
	 * @param st
	 * @param conn 
	 * void 
	 * @author rose
	 */
	public static void close(Statement st, Connection conn) {
		try {
			if (st != null)
				st.close();
			if (conn != null)
				conn.close();
		} catch (SQLException e) {
			LogUtils.printlnErrorToFile("[连接关闭失败!]");
			GlobalContent.flag = true;
			e.printStackTrace();
		}
		LogUtils.printlnToFile("[连接关闭成功!]");
	}

	/**
	 * 关闭连接
	 * @param rs
	 * @param st
	 * @param conn 
	 * void 
	 * @author rose
	 */
	public static void close(ResultSet rs, Statement st, Connection conn) {
		try {
			if (rs != null)
				rs.close();
			if (st != null)
				st.close();
			if (conn != null)
				conn.close();
		} catch (SQLException e) {
			LogUtils.printlnErrorToFile("[连接关闭失败!]");
			GlobalContent.flag = true;
			e.printStackTrace();
		}
		LogUtils.printlnToFile("[连接关闭成功!]");
	}
}
